﻿@using Smartstore.Web.Models.Catalog
@using Smartstore.Core.Common
@using Smartstore.Core.Catalog.Pricing

@model ProductDetailsModel

@{
    if (Model.AddToCart.DisableBuyButton && Model.AddToCart.DisableWishlistButton && !Model.Price.ShowLoginNote)
    {
        return;
    }

    async Task RenderPromoPriceLine(ComparePriceModel priceModel)
    {
        <div sm-if="priceModel != null" class="pd-promoprice-line">
            <span class="pd-promoprice-label">@(priceModel.Label):</span>
            <span class="pd-promoprice pl-1">@Html.Raw(priceModel.Price)</span>
            <a sm-if="priceModel.Description.HasValue()" href="javascript:;" 
               role="button" 
               class="pd-promoprice-tip pd-popover-trigger" 
               data-toggle="popover"
               data-container="body"
               data-placement="top"
               data-trigger="focus" 
               data-content="@priceModel.Description">
                <i class="fa fa-circle-info"></i>
            </a>
        </div>
    }
}

<div class="pd-offer-price" sm-suppress-if-empty="true">
    @if (Model.Price.ShowLoginNote)
    {
        <h4 class="pd-price pd-loginforprice">@T("Products.LoginForPrice")</h4>
    }
    else if (Model.Price.CallForPrice)
    {
        <h4 class="pd-price pd-callforprice">@T("Products.CallForPrice")</h4>
    }
    else
    {
        var isBundlePart = Model.IsBundlePart;
        var priceModel = Model.Price;
        var basePriceEnabled = isBundlePart ? priceModel.BundleItemShowBasePrice : priceModel.IsBasePriceEnabled;
        var isBundleItemPricing = Model.BundlePerItemPricing;
        var showPriceSaving = priceModel.Saving.SavingAmount.HasValue && (priceModel.ShowRetailPriceSaving || priceModel.RegularPrice != null);
        var showPrice = !(isBundlePart && priceModel.FinalPrice == 0);

        @* Price *@
        <div class="pd-group" sm-suppress-if-empty="true">
            <div class="pd-price-group mb-3" sm-suppress-if-empty="true">

                @* Promo badges *@
                <div sm-if="priceModel.Badges.Count > 0" class="pd-promobadges">
                    @foreach (var badge in priceModel.Badges.OrderBy(x => x.DisplayOrder))
                    {
                        <span class="badge badge-@badge.Style">@badge.Label</span>
                    }
                </div>

                @* Final price *@
                <div sm-if="showPrice" class="pd-finalprice" attr-class='(priceModel.Saving.HasSaving, "pd-finalprice-offer")'>
                    <meta itemprop="priceCurrency" sm-if="!isBundlePart" content="@WorkContext.WorkingCurrency.CurrencyCode" />
                    <meta itemprop="price" sm-if="!priceModel.CallForPrice && !isBundlePart" content="@priceModel.FinalPrice.RoundedAmount.ToStringInvariant()" />
                    @if (priceModel.ValidUntilUtc.HasValue)
                    {
                        <meta itemprop="priceValidUntil" content="@priceModel.ValidUntilUtc.Value.ToString("u")" />
                    }
                    <span class="pd-finalprice-amount" attr-class='(isBundlePart, "pd-finalprice-sm")'>
                        @Html.Raw(priceModel.FinalPrice)
                    </span>
                    <span sm-if="priceModel.CountdownText != null" class="pd-offer-countdown">
                        @Html.Raw(priceModel.CountdownText)
                    </span>
                </div>

                @* Compare prices *@
                <div sm-if="priceModel.RegularPrice != null || priceModel.RetailPrice != null" class="pd-promoprice-group">
                    @{
                        // Regular price
                        await RenderPromoPriceLine(priceModel.RegularPrice);

                        // Retail price
                        await RenderPromoPriceLine(priceModel.RetailPrice);
                    }

                    @* You save *@
                    <div sm-if="showPriceSaving" class="pd-promoprice-line">
                        <span class="pd-promoprice-label">@T("ShoppingCart.ItemYouSave"):</span>
                        <span class="pd-promosaving pl-1 d-flex">
                            <span>@priceModel.Saving.SavingAmount.Value</span>
                            <span class="badge badge-danger pd-saving-badge">@T("Products.SavingBadgeLabel", priceModel.Saving.SavingPercent.ToString("N0"))</span>
                        </span>
                    </div>
                </div>
            </div>

            <zone name="@("productdetails_add_info{0}".FormatWith(isBundlePart ? "_bundle" : string.Empty))" />
        </div>

        <div sm-if="!isBundlePart" class="pd-tierprices">
            <!-- Tier prices -->
            <partial name="Product.TierPrices" model="Model.Price.TierPrices" />
        </div>

        <div class="pd-group" sm-if="(basePriceEnabled && priceModel.BasePriceInfo.HasValue()) || (Model.ShowLegalInfo && Model.LegalInfo.HasValue())">
            <!-- Pangv & legal info -->
            <div class="pd-pangv" sm-if="basePriceEnabled && priceModel.BasePriceInfo.HasValue()">
                @Html.Raw(LocalizedValue.FixBrackets(priceModel.BasePriceInfo, WorkContext.WorkingLanguage))
            </div>
            <div class="pd-legalinfo" sm-if="Model.ShowLegalInfo && Model.LegalInfo.HasValue() && priceModel.FinalPrice != 0">
                @Html.Raw(Model.LegalInfo)
            </div>
        </div>
    }
</div>